import React, {Component} from 'react';
import {
    SafeAreaView,
    View,
    Text,
    StyleSheet,
    Button,
    Dimensions,
    ScrollView,
    TouchableOpacity,
    Alert,
} from 'react-native';

import getters from '../../../store/getters';

import {getToken, removeToken, setToken} from '../../../utils/Auth';
import {connect} from 'react-redux';

import {Set_Token} from '../../../store/actions/user';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import {Agenda,LocaleConfig} from 'react-native-calendars';
import AllStyle from '../../../style';
import {TabItem} from '../../../componet/settingComponent/TabItem';
import {CardItem} from '../../../componet/settingComponent/CardItem';
import {Loading} from '../../../utils/Loading';
import {getUserInfo, logout} from '../../../api/user';
import {Toast} from '../../../utils/Toast';
import {init} from 'react-native-amap-geolocation/lib/js';
import {isAuth} from '../../../storage/permissions';

LocaleConfig.locales['fr'] = {
    monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
    monthNamesShort: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
    dayNames: ['星期日','星期一', '星期二', '星期三', '星期四', '星期五', '星期六' ],
    dayNamesShort: ['日','一', '二', '三', '四', '五', '六'],
    today: "today"
};
LocaleConfig.defaultLocale = 'fr';


const width = Dimensions.get('window').width;
const height = Dimensions.get('window').height;
const Stack = createNativeStackNavigator();
class SettingsInfoScreen extends Component {
    constructor() {
        super();
        this.state={
            user: {
                'userId': null,
                'username': '',
                'password': '',
                'salt': '',
                'email': '',
                'mobile': '',
                'identityCard': null,
                'status': 1,
                'roleIdList': null,
                'createUserId': 1,
                'createTime': '',
            },
            facedatabase:false,
            ClassesManege:false,
            SignPlanGroup:false,
        }
    }
   async componentDidMount() {
        let facedatabase= await isAuth('ad:facedatabase:main')
       let ClassesManege= await isAuth('ad:ClassesManege:main')
       let SignPlanGroup= await isAuth('ad:SignPlanGroup:main')
       this.setState({facedatabase})
       this.setState({ClassesManege})

       this.setState({SignPlanGroup})


       this.init()
    }

    init(){
        Loading.show();
        getUserInfo().then(res => {
            Loading.hidden();
            let {user} = res;
            this.setState({user});

        }).catch(err => {
                Toast.showWarning(err.toString());
                Loading.hidden();
            },
        ).finally(() => {
            Loading.hidden();
        });
    }
    removeToken = () => {
        removeToken();
        //console.log(res);
    };
    setToken = () => {
        setToken();
        //console.log(res);
    };
    test = () => {
        this.props.logout();
    };
    LoadingTest=()=>{
        //alert(1)
        Loading.show()
    }
    goto=(name,params)=>{
        const route = {
            name: name,
            params: {
                routePathName: this.props.route.name,
                ...params,
            },

        };
        this.props.navigation.navigate(route);
    }
    //退出登陆
    btnLogout = () => {
        Alert.alert('退出账号', '确定退出当前账号', [{
            text: '取消', onPress: () => {
            },
        }, {
            text: '确定', onPress: () => {
                Loading.show();
                logout().then(res => {
                    if (res.code == 0) {
                        removeToken();//清除token
                        this.props.logout();//清除状态
                        Loading.hidden();
                    }

                }).catch(err => {
                    Loading.hidden();
                    Toast.showWarning(err.toString())

                }).finally(()=>{
                    Loading.hidden();
                });
            },
        }]);

    };

    render() {
        return (
            <SafeAreaView edges={['top', 'left', 'right', 'bottom']} style={{flex: 1, backgroundColor: '#ffffff'}}>
                <View style={AllStyle.container}>
                    <View style={style.btLine}>
                        <View style={style.con_top}>
                            <Text style={style.title}>设置</Text>
                        </View>
                    </View>
                    <View style={{padding: 15}}>
                        <TouchableOpacity onPress={()=>{
                            this.goto('mySetting',{title: '个人信息'})
                        }}>
                            <View style={[AllStyle.card]} >
                                <CardItem title={'我的设置'}></CardItem>
                            </View>
                        </TouchableOpacity>
                        <TouchableOpacity onPress={()=>{
                            this.goto('faceRecord',{title:'人脸录入',userId:this.state.user.userId})
                        }}>
                            <View style={[AllStyle.card]} >
                                <CardItem title={'人脸录入'} value={''}></CardItem>
                            </View>
                        </TouchableOpacity>
                        {
                            this.state.facedatabase&&
                        <TouchableOpacity onPress={()=>{
                            this.goto('faceManagement',{title:'人脸管理'})
                        }}>
                            <View style={[AllStyle.card]} >
                                <CardItem title={'人脸管理'}></CardItem>
                            </View>
                        </TouchableOpacity>
                        }
                        {
                            this.state.ClassesManege&&
                        <TouchableOpacity onPress={()=>{
                            this.goto('ClassesManege', {
                                title: '班次管理' })
                        }}>
                            <View style={[AllStyle.card]} >
                                <CardItem title={'班次管理'}></CardItem>
                            </View>

                        </TouchableOpacity>}
                        {
                            this.state.SignPlanGroup&&
                        <TouchableOpacity onPress={()=>{
                            this.props.navigation.navigate('SignPlanGroup', {title: '考勤方案组' })
                        }}>
                            <View style={[AllStyle.card]} >
                                <CardItem title={'考勤组管理'}></CardItem>
                            </View>
                        </TouchableOpacity>}
                        <TouchableOpacity onPress={()=>{
                            this.btnLogout()
                        }}>
                            <View style={[AllStyle.card]} >
                                <CardItem title={'退出登陆'}></CardItem>
                            </View>
                        </TouchableOpacity>
                    </View>

                </View>
            </SafeAreaView>
        );
    }


};
const style = StyleSheet.create({
    con_top: {
        backgroundColor: '#fff',
        // borderBottomWidth: 1,
        // borderStyle: 'solid',
        // borderColor: '#E8E8EA',
        width: width,
        padding: '2%',
        flexDirection: 'row',
    },
    title: {
        paddingLeft:10,
        fontSize: 19,
        fontWeight:'bold'
    },
    btLine:{
        marginBottom: StyleSheet.hairlineWidth,

        // 【方法一】用border做分割线
        borderBottomColor: '#EBEDED',
        borderBottomWidth: StyleSheet.hairlineWidth,
    },
    card_top: {
        paddingBottom: 15,
    },
    card_top_text: {
        fontSize: 18,
        fontWeight: 'bold',
    },

});
const dispatchToProps = (dispatch) => {
    return {
        logout() {
            //清空状态
            let action = Set_Token(null);
            dispatch(action);
        },
    };
};
export default connect(null, dispatchToProps)(SettingsInfoScreen);

//export default SettingsInfoScreen
